{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "65332cc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------------\n",
      "Structure of the actions for Gaugefields\n",
      "num. of terms: 1\n",
      "-------------------------------\n",
      "      1-st term: \n",
      "          coefficient: 2.85\n",
      "      -------------------------\n",
      "1-st loop\n",
      "L\"$U_{1}(n)U_{2}(n+e_{1})U^{\\dagger}_{1}(n+e_{2})U^{\\dagger}_{2}(n)$\"\t\n",
      "2-nd loop\n",
      "L\"$U_{1}(n)U_{3}(n+e_{1})U^{\\dagger}_{1}(n+e_{3})U^{\\dagger}_{3}(n)$\"\t\n",
      "3-rd loop\n",
      "L\"$U_{1}(n)U_{4}(n+e_{1})U^{\\dagger}_{1}(n+e_{4})U^{\\dagger}_{4}(n)$\"\t\n",
      "4-th loop\n",
      "L\"$U_{2}(n)U_{3}(n+e_{2})U^{\\dagger}_{2}(n+e_{3})U^{\\dagger}_{3}(n)$\"\t\n",
      "5-th loop\n",
      "L\"$U_{2}(n)U_{4}(n+e_{2})U^{\\dagger}_{2}(n+e_{4})U^{\\dagger}_{4}(n)$\"\t\n",
      "6-th loop\n",
      "L\"$U_{3}(n)U_{4}(n+e_{3})U^{\\dagger}_{3}(n+e_{4})U^{\\dagger}_{4}(n)$\"\t\n",
      "7-th loop\n",
      "L\"$U_{2}(n)U_{1}(n+e_{2})U^{\\dagger}_{2}(n+e_{1})U^{\\dagger}_{1}(n)$\"\t\n",
      "8-th loop\n",
      "L\"$U_{3}(n)U_{1}(n+e_{3})U^{\\dagger}_{3}(n+e_{1})U^{\\dagger}_{1}(n)$\"\t\n",
      "9-th loop\n",
      "L\"$U_{4}(n)U_{1}(n+e_{4})U^{\\dagger}_{4}(n+e_{1})U^{\\dagger}_{1}(n)$\"\t\n",
      "10-th loop\n",
      "L\"$U_{3}(n)U_{2}(n+e_{3})U^{\\dagger}_{3}(n+e_{2})U^{\\dagger}_{2}(n)$\"\t\n",
      "11-th loop\n",
      "L\"$U_{4}(n)U_{2}(n+e_{4})U^{\\dagger}_{4}(n+e_{2})U^{\\dagger}_{2}(n)$\"\t\n",
      "12-th loop\n",
      "L\"$U_{4}(n)U_{3}(n+e_{4})U^{\\dagger}_{4}(n+e_{3})U^{\\dagger}_{3}(n)$\"\t\n",
      "      -------------------------\n",
      "----------------------------------------------\n",
      "Sold = -4690.423130719915, Snew = -4690.626017782982\n",
      "Snew - Sold = -0.20288706306655513\n",
      "1 plaq_t = 0.8302396738631115\n",
      "acceptance ratio 1.0\n",
      "Sold = -3224.117695773685, Snew = -3224.2225756481644\n",
      "Snew - Sold = -0.10487987447913838\n",
      "2 plaq_t = 0.7421207037178732\n",
      "acceptance ratio 1.0\n",
      "Sold = -2447.018409129706, Snew = -2447.0725315775844\n",
      "Snew - Sold = -0.054122447878398816\n",
      "3 plaq_t = 0.685366576798169\n",
      "acceptance ratio 1.0\n",
      "Sold = -1878.365444451727, Snew = -1878.4085030297156\n",
      "Snew - Sold = -0.04305857798863144\n",
      "4 plaq_t = 0.685366576798169\n",
      "acceptance ratio 0.75\n",
      "Sold = -1921.3807868505842, Snew = -1921.415389066242\n",
      "Snew - Sold = -0.03460221565774191\n",
      "5 plaq_t = 0.6456324209589348\n",
      "acceptance ratio 0.8\n",
      "Sold = -1607.6621677865978, Snew = -1607.674268417542\n",
      "Snew - Sold = -0.012100630944132718\n",
      "6 plaq_t = 0.6239495133965148\n",
      "acceptance ratio 0.8333333333333334\n",
      "Sold = -1317.4443832503503, Snew = -1317.4655512426766\n",
      "Snew - Sold = -0.02116799232635458\n",
      "7 plaq_t = 0.5967573386032624\n",
      "acceptance ratio 0.8571428571428571\n",
      "Sold = -1143.7612879052258, Snew = -1143.768207023109\n",
      "Snew - Sold = -0.006919117883171566\n",
      "8 plaq_t = 0.588495366285565\n",
      "acceptance ratio 0.875\n",
      "Sold = -1165.0391378562354, Snew = -1165.0433174285222\n",
      "Snew - Sold = -0.00417957228683008\n",
      "9 plaq_t = 0.5874424642224534\n",
      "acceptance ratio 0.8888888888888888\n",
      "Sold = -1102.776146201922, Snew = -1102.7729313001937\n",
      "Snew - Sold = 0.003214901728370023\n",
      "10 plaq_t = 0.5826299293180046\n",
      "acceptance ratio 0.9\n",
      "Sold = -925.8428857356857, Snew = -925.8524029305609\n",
      "Snew - Sold = -0.00951719487511582\n",
      "11 plaq_t = 0.560988927473026\n",
      "acceptance ratio 0.9090909090909091\n",
      "Sold = -855.7827910030323, Snew = -855.7867999320415\n",
      "Snew - Sold = -0.004008929009160056\n",
      "12 plaq_t = 0.5658272703709453\n",
      "acceptance ratio 0.9166666666666666\n",
      "Sold = -795.5718859402214, Snew = -795.574272345094\n",
      "Snew - Sold = -0.002386404872595449\n",
      "13 plaq_t = 0.568421999270564\n",
      "acceptance ratio 0.9230769230769231\n",
      "Sold = -935.6327572172527, Snew = -935.6340488261485\n",
      "Snew - Sold = -0.0012916088958263572\n",
      "14 plaq_t = 0.5668061350832706\n",
      "acceptance ratio 0.9285714285714286\n",
      "Sold = -848.6524658253866, Snew = -848.6540284330367\n",
      "Snew - Sold = -0.001562607650157588\n",
      "15 plaq_t = 0.5642343595451371\n",
      "acceptance ratio 0.9333333333333333\n",
      "Sold = -837.3568555161582, Snew = -837.3559785925336\n",
      "Snew - Sold = 0.000876923624673509\n",
      "16 plaq_t = 0.5669284765109285\n",
      "acceptance ratio 0.9375\n",
      "Sold = -754.8082717858915, Snew = -754.7992571404129\n",
      "Snew - Sold = 0.009014645478600869\n",
      "17 plaq_t = 0.5667496084363132\n",
      "acceptance ratio 0.9411764705882353\n",
      "Sold = -867.0189985722291, Snew = -867.0271258604798\n",
      "Snew - Sold = -0.008127288250761922\n",
      "18 plaq_t = 0.5691949518933692\n",
      "acceptance ratio 0.9444444444444444\n",
      "Sold = -903.7083473831799, Snew = -903.7007474696629\n",
      "Snew - Sold = 0.007599913516969536\n",
      "19 plaq_t = 0.5681639470345163\n",
      "acceptance ratio 0.9473684210526315\n",
      "Sold = -798.8272341644415, Snew = -798.833910092354\n",
      "Snew - Sold = -0.006675927912510815\n",
      "20 plaq_t = 0.5548668614165978\n",
      "acceptance ratio 0.95\n",
      "Sold = -676.0805811032487, Snew = -676.0804087107163\n",
      "Snew - Sold = 0.00017239253247680608\n",
      "21 plaq_t = 0.5477139452867905\n",
      "acceptance ratio 0.9523809523809523\n",
      "Sold = -856.323921783272, Snew = -856.3108900064653\n",
      "Snew - Sold = 0.013031776806656126\n",
      "22 plaq_t = 0.5690615086769961\n",
      "acceptance ratio 0.9545454545454546\n",
      "Sold = -846.4528927225838, Snew = -846.4599389666646\n",
      "Snew - Sold = -0.007046244080811448\n",
      "23 plaq_t = 0.5611985246939649\n",
      "acceptance ratio 0.9565217391304348\n",
      "Sold = -728.6910609179859, Snew = -728.6930021142371\n",
      "Snew - Sold = -0.001941196251209476\n",
      "24 plaq_t = 0.5521105521819563\n",
      "acceptance ratio 0.9583333333333334\n",
      "Sold = -829.838376921547, Snew = -829.8387436414232\n",
      "Snew - Sold = -0.0003667198761831969\n",
      "25 plaq_t = 0.5552266369363223\n",
      "acceptance ratio 0.96\n",
      "Sold = -731.6163668676236, Snew = -731.6159636628099\n",
      "Snew - Sold = 0.0004032048136650701\n",
      "26 plaq_t = 0.5578875231174508\n",
      "acceptance ratio 0.9615384615384616\n",
      "Sold = -707.0510715998334, Snew = -707.0591607813712\n",
      "Snew - Sold = -0.00808918153779814\n",
      "27 plaq_t = 0.5363832881278828\n",
      "acceptance ratio 0.9629629629629629\n",
      "Sold = -510.42792707503486, Snew = -510.41865562993553\n",
      "Snew - Sold = 0.009271445099329867\n",
      "28 plaq_t = 0.5434487955395565\n",
      "acceptance ratio 0.9642857142857143\n",
      "Sold = -557.6681932386473, Snew = -557.6741256996547\n",
      "Snew - Sold = -0.005932461007432721\n",
      "29 plaq_t = 0.5417603356070694\n",
      "acceptance ratio 0.9655172413793104\n",
      "Sold = -613.0082615967103, Snew = -613.003473367201\n",
      "Snew - Sold = 0.0047882295093586436\n",
      "30 plaq_t = 0.5514908052397194\n",
      "acceptance ratio 0.9666666666666667\n",
      "Sold = -759.5999063672298, Snew = -759.5930050571651\n",
      "Snew - Sold = 0.006901310064677091\n",
      "31 plaq_t = 0.5654008932956306\n",
      "acceptance ratio 0.967741935483871\n",
      "Sold = -833.7383135741356, Snew = -833.7420443570536\n",
      "Snew - Sold = -0.0037307829179553664\n",
      "32 plaq_t = 0.5674326797124811\n",
      "acceptance ratio 0.96875\n",
      "Sold = -849.3868576309187, Snew = -849.3939133354183\n",
      "Snew - Sold = -0.007055704499634885\n",
      "33 plaq_t = 0.5600075994066024\n",
      "acceptance ratio 0.9696969696969697\n",
      "Sold = -830.812154931958, Snew = -830.8148455884525\n",
      "Snew - Sold = -0.0026906564944511047\n",
      "34 plaq_t = 0.5618390004266676\n",
      "acceptance ratio 0.9705882352941176\n",
      "Sold = -892.5059356190059, Snew = -892.4988869126546\n",
      "Snew - Sold = 0.007048706351270084\n",
      "35 plaq_t = 0.5772004793602825\n",
      "acceptance ratio 0.9714285714285714\n",
      "Sold = -906.9048210519695, Snew = -906.9069572282865\n",
      "Snew - Sold = -0.002136176316980709\n",
      "36 plaq_t = 0.5634755063402519\n",
      "acceptance ratio 0.9722222222222222\n",
      "Sold = -923.9455338907765, Snew = -923.9534780192143\n",
      "Snew - Sold = -0.007944128437884501\n",
      "37 plaq_t = 0.5643213203467232\n",
      "acceptance ratio 0.972972972972973\n",
      "Sold = -844.1713467733425, Snew = -844.1695247008515\n",
      "Snew - Sold = 0.0018220724909951969\n",
      "38 plaq_t = 0.561956628094397\n",
      "acceptance ratio 0.9736842105263158\n",
      "Sold = -857.4049580900623, Snew = -857.4033299725279\n",
      "Snew - Sold = 0.0016281175344374788\n",
      "39 plaq_t = 0.566307485853467\n",
      "acceptance ratio 0.9743589743589743\n",
      "Sold = -963.2803249893632, Snew = -963.2726675768754\n",
      "Snew - Sold = 0.007657412487787951\n",
      "40 plaq_t = 0.5671013683847449\n",
      "acceptance ratio 0.975\n",
      "Sold = -812.1718989273468, Snew = -812.1738479500191\n",
      "Snew - Sold = -0.0019490226723064552\n",
      "41 plaq_t = 0.5702309840605199\n",
      "acceptance ratio 0.975609756097561\n",
      "Sold = -934.8998972688269, Snew = -934.9021020482623\n",
      "Snew - Sold = -0.0022047794354875805\n",
      "42 plaq_t = 0.5752947031367512\n",
      "acceptance ratio 0.9761904761904762\n",
      "Sold = -1007.2001956099475, Snew = -1007.2032464287399\n",
      "Snew - Sold = -0.003050818792416976\n",
      "43 plaq_t = 0.5749257183828951\n",
      "acceptance ratio 0.9767441860465116\n",
      "Sold = -893.2630606336752, Snew = -893.2660690698312\n",
      "Snew - Sold = -0.003008436156051175\n",
      "44 plaq_t = 0.562022070954701\n",
      "acceptance ratio 0.9772727272727273\n",
      "Sold = -813.9123310467312, Snew = -813.9084866503808\n",
      "Snew - Sold = 0.0038443963503596024\n",
      "45 plaq_t = 0.568254515491284\n",
      "acceptance ratio 0.9777777777777777\n",
      "Sold = -1013.9023912901939, Snew = -1013.8994228033835\n",
      "Snew - Sold = 0.0029684868104595807\n",
      "46 plaq_t = 0.5736993853122777\n",
      "acceptance ratio 0.9782608695652174\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sold = -880.8083511453724, Snew = -880.8034766317405\n",
      "Snew - Sold = 0.004874513631875743\n",
      "47 plaq_t = 0.5654179616785627\n",
      "acceptance ratio 0.9787234042553191\n",
      "Sold = -987.2486917656056, Snew = -987.2456560694109\n",
      "Snew - Sold = 0.0030356961947290984\n",
      "48 plaq_t = 0.5873280930423245\n",
      "acceptance ratio 0.9791666666666666\n",
      "Sold = -930.3954181405197, Snew = -930.3982087036748\n",
      "Snew - Sold = -0.0027905631550311227\n",
      "49 plaq_t = 0.5739243321999938\n",
      "acceptance ratio 0.9795918367346939\n",
      "Sold = -892.1125189624181, Snew = -892.1122211288621\n",
      "Snew - Sold = 0.000297833556032856\n",
      "50 plaq_t = 0.5723523880760538\n",
      "acceptance ratio 0.98\n",
      "Sold = -910.471079446409, Snew = -910.4746417041588\n",
      "Snew - Sold = -0.0035622577497633756\n",
      "51 plaq_t = 0.5680319454953126\n",
      "acceptance ratio 0.9803921568627451\n",
      "Sold = -888.1571704212165, Snew = -888.1519827808252\n",
      "Snew - Sold = 0.0051876403913411195\n",
      "52 plaq_t = 0.5699527835363045\n",
      "acceptance ratio 0.9807692307692307\n",
      "Sold = -941.9593408178557, Snew = -941.9570441638116\n",
      "Snew - Sold = 0.002296654044130264\n",
      "53 plaq_t = 0.5805581823387868\n",
      "acceptance ratio 0.9811320754716981\n",
      "Sold = -918.0914574586968, Snew = -918.0933648884893\n",
      "Snew - Sold = -0.0019074297924817074\n",
      "54 plaq_t = 0.5703372476806934\n",
      "acceptance ratio 0.9814814814814815\n",
      "Sold = -951.6892734892763, Snew = -951.6960843814277\n",
      "Snew - Sold = -0.006810892151406733\n",
      "55 plaq_t = 0.5646399193414089\n",
      "acceptance ratio 0.9818181818181818\n",
      "Sold = -908.3530244738877, Snew = -908.3568283000282\n",
      "Snew - Sold = -0.0038038261404835794\n",
      "56 plaq_t = 0.5631464550386656\n",
      "acceptance ratio 0.9821428571428571\n",
      "Sold = -815.9647335224199, Snew = -815.9665904401663\n",
      "Snew - Sold = -0.001856917746408726\n",
      "57 plaq_t = 0.5548759818573108\n",
      "acceptance ratio 0.9824561403508771\n",
      "Sold = -781.6994360358585, Snew = -781.6997212546985\n",
      "Snew - Sold = -0.00028521883996290853\n",
      "58 plaq_t = 0.5586291580345607\n",
      "acceptance ratio 0.9827586206896551\n",
      "Sold = -793.8216975040241, Snew = -793.8226386753336\n",
      "Snew - Sold = -0.0009411713094777951\n",
      "59 plaq_t = 0.5511194132094923\n",
      "acceptance ratio 0.9830508474576272\n",
      "Sold = -793.6401915352553, Snew = -793.6381583538523\n",
      "Snew - Sold = 0.0020331814030214446\n",
      "60 plaq_t = 0.5618967429541751\n",
      "acceptance ratio 0.9833333333333333\n",
      "Sold = -844.4923298365452, Snew = -844.4894857725412\n",
      "Snew - Sold = 0.0028440640039661957\n",
      "61 plaq_t = 0.5578678960418415\n",
      "acceptance ratio 0.9836065573770492\n",
      "Sold = -650.7170249677474, Snew = -650.7209363804022\n",
      "Snew - Sold = -0.003911412654815649\n",
      "62 plaq_t = 0.5443625535491028\n",
      "acceptance ratio 0.9838709677419355\n",
      "Sold = -762.3807623394923, Snew = -762.3771501652172\n",
      "Snew - Sold = 0.0036121742750765407\n",
      "63 plaq_t = 0.5603682506426012\n",
      "acceptance ratio 0.9841269841269841\n",
      "Sold = -663.3913107422113, Snew = -663.4037155172005\n",
      "Snew - Sold = -0.012404774989136058\n",
      "64 plaq_t = 0.54139470135618\n",
      "acceptance ratio 0.984375\n",
      "Sold = -639.0378950019476, Snew = -639.0334021524532\n",
      "Snew - Sold = 0.004492849494454276\n",
      "65 plaq_t = 0.5415283422724194\n",
      "acceptance ratio 0.9846153846153847\n",
      "Sold = -637.584907552924, Snew = -637.5780542908014\n",
      "Snew - Sold = 0.006853262122604065\n",
      "66 plaq_t = 0.557634907540782\n",
      "acceptance ratio 0.9848484848484849\n",
      "Sold = -802.6925866916163, Snew = -802.6996687540186\n",
      "Snew - Sold = -0.007082062402332667\n",
      "67 plaq_t = 0.5485425836721745\n",
      "acceptance ratio 0.9850746268656716\n",
      "Sold = -791.3541206887321, Snew = -791.3548246956466\n",
      "Snew - Sold = -0.0007040069144750305\n",
      "68 plaq_t = 0.5580309904409372\n",
      "acceptance ratio 0.9852941176470589\n",
      "Sold = -912.8015079449387, Snew = -912.8055499961251\n",
      "Snew - Sold = -0.004042051186388562\n",
      "69 plaq_t = 0.5564461967985063\n",
      "acceptance ratio 0.9855072463768116\n",
      "Sold = -683.97740394635, Snew = -683.9845376714538\n",
      "Snew - Sold = -0.007133725103813049\n",
      "70 plaq_t = 0.5475411906762626\n",
      "acceptance ratio 0.9857142857142858\n",
      "Sold = -593.2823744806228, Snew = -593.2831938549416\n",
      "Snew - Sold = -0.0008193743187803193\n",
      "71 plaq_t = 0.5472565005314122\n",
      "acceptance ratio 0.9859154929577465\n",
      "Sold = -743.3901249298087, Snew = -743.3928657408205\n",
      "Snew - Sold = -0.002740811011790356\n",
      "72 plaq_t = 0.5478306112571286\n",
      "acceptance ratio 0.9861111111111112\n",
      "Sold = -662.5661941463159, Snew = -662.5594567115859\n",
      "Snew - Sold = 0.00673743472998467\n",
      "73 plaq_t = 0.5513654530081086\n",
      "acceptance ratio 0.9863013698630136\n",
      "Sold = -670.1355125650962, Snew = -670.1387798695823\n",
      "Snew - Sold = -0.003267304486143985\n",
      "74 plaq_t = 0.5465223828254253\n",
      "acceptance ratio 0.9864864864864865\n",
      "Sold = -694.0951201683984, Snew = -694.0898731300817\n",
      "Snew - Sold = 0.005247038316610997\n",
      "75 plaq_t = 0.5470220470084156\n",
      "acceptance ratio 0.9866666666666667\n",
      "Sold = -629.8211719802248, Snew = -629.8205820921457\n",
      "Snew - Sold = 0.0005898880790482508\n",
      "76 plaq_t = 0.5474629893284049\n",
      "acceptance ratio 0.9868421052631579\n",
      "Sold = -726.2360921850054, Snew = -726.245866940063\n",
      "Snew - Sold = -0.009774755057605944\n",
      "77 plaq_t = 0.5437492012550115\n",
      "acceptance ratio 0.987012987012987\n",
      "Sold = -692.3072864196029, Snew = -692.2938862138117\n",
      "Snew - Sold = 0.013400205791185726\n",
      "78 plaq_t = 0.5531776767473938\n",
      "acceptance ratio 0.9871794871794872\n",
      "Sold = -738.3212331674713, Snew = -738.3173414092525\n",
      "Snew - Sold = 0.003891758218742325\n",
      "79 plaq_t = 0.5620481622117752\n",
      "acceptance ratio 0.9873417721518988\n",
      "Sold = -887.8168982269012, Snew = -887.81455495517\n",
      "Snew - Sold = 0.002343271731206187\n",
      "80 plaq_t = 0.5735093213621414\n",
      "acceptance ratio 0.9875\n",
      "Sold = -972.4513335056372, Snew = -972.4556212751659\n",
      "Snew - Sold = -0.00428776952867338\n",
      "81 plaq_t = 0.5684200525920813\n",
      "acceptance ratio 0.9876543209876543\n",
      "Sold = -873.1299636839803, Snew = -873.1348971990583\n",
      "Snew - Sold = -0.004933515077937045\n",
      "82 plaq_t = 0.5466468065766467\n",
      "acceptance ratio 0.9878048780487805\n",
      "Sold = -816.6803825033476, Snew = -816.6769293504149\n",
      "Snew - Sold = 0.003453152932706871\n",
      "83 plaq_t = 0.5640252093843588\n",
      "acceptance ratio 0.9879518072289156\n",
      "Sold = -866.6539621832408, Snew = -866.649890748241\n",
      "Snew - Sold = 0.004071434999787016\n",
      "84 plaq_t = 0.5690740221477318\n",
      "acceptance ratio 0.9880952380952381\n",
      "Sold = -843.0744643447651, Snew = -843.0748825039796\n",
      "Snew - Sold = -0.0004181592144050228\n",
      "85 plaq_t = 0.5565688321746187\n",
      "acceptance ratio 0.9882352941176471\n",
      "Sold = -744.9741337549885, Snew = -744.9718547968505\n",
      "Snew - Sold = 0.0022789581380493473\n",
      "86 plaq_t = 0.562561693255074\n",
      "acceptance ratio 0.9883720930232558\n",
      "Sold = -917.7651415122277, Snew = -917.7604705554081\n",
      "Snew - Sold = 0.004670956819609273\n",
      "87 plaq_t = 0.5679133666322552\n",
      "acceptance ratio 0.9885057471264368\n",
      "Sold = -822.3954838629334, Snew = -822.4009092857127\n",
      "Snew - Sold = -0.005425422779353539\n",
      "88 plaq_t = 0.5539675478676453\n",
      "acceptance ratio 0.9886363636363636\n",
      "Sold = -717.1042966238319, Snew = -717.1042837187879\n",
      "Snew - Sold = 1.2905044059152715e-5\n",
      "89 plaq_t = 0.5576283585623457\n",
      "acceptance ratio 0.9887640449438202\n",
      "Sold = -670.0936081710324, Snew = -670.0897694405676\n",
      "Snew - Sold = 0.0038387304648495046\n",
      "90 plaq_t = 0.5498008498644747\n",
      "acceptance ratio 0.9888888888888889\n",
      "Sold = -754.6452486224798, Snew = -754.6537493547512\n",
      "Snew - Sold = -0.008500732271386369\n",
      "91 plaq_t = 0.5556892860290176\n",
      "acceptance ratio 0.989010989010989\n",
      "Sold = -718.7015845491296, Snew = -718.7039594229955\n",
      "Snew - Sold = -0.0023748738658468938\n",
      "92 plaq_t = 0.5539448794860984\n",
      "acceptance ratio 0.9891304347826086\n",
      "Sold = -759.5102068793221, Snew = -759.5065738841404\n",
      "Snew - Sold = 0.0036329951817606343\n",
      "93 plaq_t = 0.5639484380408322\n",
      "acceptance ratio 0.989247311827957\n",
      "Sold = -811.0167320790224, Snew = -811.0175783253817\n",
      "Snew - Sold = -0.0008462463592877612\n",
      "94 plaq_t = 0.5597109996184891\n",
      "acceptance ratio 0.9893617021276596\n",
      "Sold = -791.310135973411, Snew = -791.3112763878144\n",
      "Snew - Sold = -0.0011404144033804187\n",
      "95 plaq_t = 0.5609355729605147\n",
      "acceptance ratio 0.9894736842105263\n",
      "Sold = -742.4435000217154, Snew = -742.4458194934359\n",
      "Snew - Sold = -0.002319471720511501\n",
      "96 plaq_t = 0.5580874917101363\n",
      "acceptance ratio 0.9895833333333334\n",
      "Sold = -871.9559705347065, Snew = -871.9515735521436\n",
      "Snew - Sold = 0.004396982562866469\n",
      "97 plaq_t = 0.5682022342798211\n",
      "acceptance ratio 0.9896907216494846\n",
      "Sold = -918.2305612804394, Snew = -918.2230498806157\n",
      "Snew - Sold = 0.007511399823670217\n",
      "98 plaq_t = 0.5737211234819649\n",
      "acceptance ratio 0.9897959183673469\n",
      "Sold = -1007.4013765775462, Snew = -1007.4052862977323\n",
      "Snew - Sold = -0.003909720186129562\n",
      "99 plaq_t = 0.5757629267934254\n",
      "acceptance ratio 0.98989898989899\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sold = -910.4445591201957, Snew = -910.449693755118\n",
      "Snew - Sold = -0.005134634922342229\n",
      "100 plaq_t = 0.560141771702847\n",
      "acceptance ratio 0.99\n"
     ]
    }
   ],
   "source": [
    "using Gaugefields\n",
    "using LinearAlgebra\n",
    "using LaTeXStrings\n",
    "\n",
    "function MDtest!(gauge_action,U,Dim)\n",
    "    p = initialize_TA_Gaugefields(U) #This is a traceless-antihermitian gauge fields. This has NC^2-1 real coefficients. \n",
    "    Uold = similar(U)\n",
    "    substitute_U!(Uold,U)\n",
    "    MDsteps = 100\n",
    "    temp1 = similar(U[1])\n",
    "    temp2 = similar(U[1])\n",
    "    comb = 6\n",
    "    factor = 1/(comb*U[1].NV*U[1].NC)\n",
    "    numaccepted = 0\n",
    "\n",
    "    numtrj = 100\n",
    "    for itrj = 1:numtrj\n",
    "        accepted = MDstep!(gauge_action,U,p,MDsteps,Dim,Uold)\n",
    "        numaccepted += ifelse(accepted,1,0)\n",
    "\n",
    "        plaq_t = calculate_Plaquette(U,temp1,temp2)*factor\n",
    "        println(\"$itrj plaq_t = $plaq_t\")\n",
    "        println(\"acceptance ratio \",numaccepted/itrj)\n",
    "    end\n",
    "end\n",
    "\n",
    "function calc_action(gauge_action,U,p)\n",
    "    NC = U[1].NC\n",
    "    Sg = -evaluate_GaugeAction(gauge_action,U)/NC #evaluate_GaugeAction(gauge_action,U) = tr(evaluate_GaugeAction_untraced(gauge_action,U))\n",
    "    Sp = p*p/2\n",
    "    S = Sp + Sg\n",
    "    return real(S)\n",
    "end\n",
    "\n",
    "\n",
    "function MDstep!(gauge_action,U,p,MDsteps,Dim,Uold)\n",
    "    Δτ = 1/MDsteps\n",
    "    gauss_distribution!(p)\n",
    "    Sold = calc_action(gauge_action,U,p)\n",
    "    substitute_U!(Uold,U)\n",
    "\n",
    "    for itrj=1:MDsteps\n",
    "        U_update!(U,p,0.5,Δτ,Dim,gauge_action)\n",
    "\n",
    "        P_update!(U,p,1.0,Δτ,Dim,gauge_action)\n",
    "\n",
    "        U_update!(U,p,0.5,Δτ,Dim,gauge_action)\n",
    "    end\n",
    "    Snew = calc_action(gauge_action,U,p)\n",
    "    println(\"Sold = $Sold, Snew = $Snew\")\n",
    "    println(\"Snew - Sold = $(Snew-Sold)\")\n",
    "    ratio = min(1,exp(Snew-Sold))\n",
    "    if rand() > ratio\n",
    "        substitute_U!(U,Uold)\n",
    "        return false\n",
    "    else\n",
    "        return true\n",
    "    end\n",
    "end\n",
    "\n",
    "function U_update!(U,p,ϵ,Δτ,Dim,gauge_action)\n",
    "    temps = get_temporary_gaugefields(gauge_action)\n",
    "    temp1 = temps[1]\n",
    "    temp2 = temps[2]\n",
    "    expU = temps[3]\n",
    "    W = temps[4]\n",
    "\n",
    "    for μ=1:Dim\n",
    "        exptU!(expU,ϵ*Δτ,p[μ],[temp1,temp2])\n",
    "        mul!(W,expU,U[μ])\n",
    "        substitute_U!(U[μ],W)\n",
    "        \n",
    "    end\n",
    "end\n",
    "\n",
    "function P_update!(U,p,ϵ,Δτ,Dim,gauge_action) # p -> p +factor*U*dSdUμ\n",
    "    NC = U[1].NC\n",
    "    temps = get_temporary_gaugefields(gauge_action)\n",
    "    dSdUμ = temps[end]\n",
    "    factor =  -ϵ*Δτ/(NC)\n",
    "\n",
    "    for μ=1:Dim\n",
    "        calc_dSdUμ!(dSdUμ,gauge_action,μ,U)\n",
    "        mul!(temps[1],U[μ],dSdUμ) # U*dSdUμ\n",
    "        Traceless_antihermitian_add!(p[μ],factor,temps[1])\n",
    "    end\n",
    "end\n",
    "\n",
    "function test1()\n",
    "    NX = 4\n",
    "    NY = 4\n",
    "    NZ = 4\n",
    "    NT = 4\n",
    "    Nwing = 1\n",
    "    Dim = 4\n",
    "    NC = 3\n",
    "\n",
    "    U  =Initialize_Gaugefields(NC,Nwing,NX,NY,NZ,NT,condition = \"cold\")\n",
    "\n",
    "\n",
    "    gauge_action = GaugeAction(U)\n",
    "    plaqloop = make_loops_fromname(\"plaquette\")\n",
    "    append!(plaqloop,plaqloop')\n",
    "    β = 5.7/2\n",
    "    push!(gauge_action,β,plaqloop)\n",
    "    \n",
    "    show(gauge_action)\n",
    "\n",
    "    MDtest!(gauge_action,U,Dim)\n",
    "\n",
    "end\n",
    "\n",
    "\n",
    "test1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e48f30f4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$U_{1}(n)U_{2}(n+e_{1})U^{\\dagger}_{1}(n+e_{2})U^{\\dagger}_{2}(n)$"
      ],
      "text/plain": [
       "L\"$U_{1}(n)U_{2}(n+e_{1})U^{\\dagger}_{1}(n+e_{2})U^{\\dagger}_{2}(n)$\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L\"$U_{1}(n)U_{2}(n+e_{1})U^{\\dagger}_{1}(n+e_{2})U^{\\dagger}_{2}(n)$\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cea2940",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.6.2",
   "language": "julia",
   "name": "julia-1.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
